package org.springcloud.wisdom.spring_02_classroom.admin.manageteacher.service;

import org.springcloud.wisdom.spring_02_classroom.admin.manageteacher.dao.ManageTeacherDao;
import org.springcloud.wisdom.spring_02_classroom.bean.Page;
import org.springcloud.wisdom.spring_02_classroom.teacher.tschedule.entity.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ManageTeacherService {
    @Autowired
    ManageTeacherDao manageTeacherDao;

    //修改教师密码
    public boolean updateTeacherPassword(Teacher teacher) {
        if (teacher == null){
            return false;
        }if (teacher.getTid() == null || teacher.getTid().trim().length() == 0){
            return false;
        }if (teacher.getTpass() == null || teacher.getTpass().trim().length() == 0){
            return false;
        }
        return manageTeacherDao.updateTeacherPassword(teacher) == 1;
    }

    //基于课模糊查询教师编号和姓名的分页查看所有教师信息
    public Page<Teacher> AllTeacher(int pageNum, int pageSize,String tid,String tname){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Teacher> list= manageTeacherDao.get(start,pageSize,tid,tname);
        //获取总条目数
        int total= manageTeacherDao.CountTeachers(tid,tname);
        //计算总页数
        int pages =(int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Teacher> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }
}
